﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows;
using QLKS.Models;
using QLKS.Commands;

namespace QLKS.ViewModels
{
    public class PhongViewModel : ViewModelBase
    {
        static public DataTable GetDmPhong(string columns)
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.GetDmPhong(columns);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        static public DataTable GetDmPhong(string columns,string filter)
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.GetDmPhong(columns,filter);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        private int _LoaiThue = 0;
        public int LoaiThue
        {
            get { return _LoaiThue; }
            set
            {
                _LoaiThue = value;
                OnPropertyChanged("LoaiThue");
                if (_LoaiThue == 0)
                    Gia = GiaTheoGio;
                else
                    Gia = GiaTheoNgay;
            }
        }

        double _Gia;
        public double Gia
        {
            get { return _Gia; }
            set
            {
                _Gia = value;
                OnPropertyChanged("Gia");
            }
        }

        PhongModel _phong;
        public string MaPhong
        {
            get { return _phong.MaPhong; }
            set 
            {
                _phong.MaPhong = value;
                OnPropertyChanged("MaPhong");
            }
        }

        public int LoaiPhong
        {
            get { return _phong.LoaiPhong; }
            set
            {
                _phong.LoaiPhong = value;
                OnPropertyChanged("LoaiPhong");
            }
        }
        public string TenLoaiPhong
        {
            get { return _phong.TenLoaiPhong; }
            set
            {
                _phong.TenLoaiPhong = value;
                OnPropertyChanged("TenLoaiPhong");
            }
        }
        public int Tang
        {
            get { return _phong.Tang; }
            set
            {
                _phong.Tang = value;
                OnPropertyChanged("Tang");
            }
        }
        public int ViTri
        {
            get { return _phong.ViTri; }
            set
            {
                _phong.ViTri = value;
                OnPropertyChanged("ViTri");
            }
        }
        public int SucChua
        {
            get { return _phong.SucChua; }
            set
            {
                _phong.SucChua = value;
                OnPropertyChanged("SucChua");
            }
        }
        public int TinhTrang
        {
            get { return _phong.TinhTrang; }
            set
            {
                _phong.TinhTrang = value;
                OnPropertyChanged("TinhTrang");
            }
        }
        public string TenTinhTrang
        {
            get { return _phong.TenTinhTrang; }
            set
            {
                _phong.TenTinhTrang = value;
                OnPropertyChanged("TenTinhTrang");
            }
        }
        public string GhiChu
        {
            get { return _phong.GhiChu; }
            set
            {
                _phong.GhiChu = value;
                OnPropertyChanged("GhiChu");
            }
        }
        public double GiaTheoGio
        {
            get { return _phong.GiaTheoGio; }
        }

        public double GiaTheoNgay
        {
            get { return _phong.GiaTheoNgay; }
        }

        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }

        public PhongViewModel(PhongModel ph)
        {
            _phong = ph;
            _Gia = GiaTheoGio;
        }

        private DelegateCommand _thuePhongCommand;
        private DelegateCommand _traPhongCommand;

        public DelegateCommand ThuePhongCommand
        {
            get 
            {
                if (_thuePhongCommand == null)
                    _thuePhongCommand = new DelegateCommand(ThuePhong, CanThuePhong);
                return _thuePhongCommand; 
            }
        }
        public DelegateCommand TraPhongCommand
        {
            get
            {
                if (_traPhongCommand == null)
                    _traPhongCommand = new DelegateCommand(TraPhong, CanTraPhong);
                return _traPhongCommand;
            }
        }


        bool CanThuePhong() { return TinhTrang == 1; }
        bool CanTraPhong() { return TinhTrang == 2; }
        void ThuePhong() 
        {
            Views.frmThuePhong frm = new Views.frmThuePhong(this, Views.Command.New);
            frm.DataContext = this;
            frm.ShowDialog();
        }
        void TraPhong() 
        {
            Views.frmThanhToan frm = new Views.frmThanhToan(this, Views.Command.New);
            frm.DataContext = this;
            frm.ShowDialog();
        }

        static public DataTable GetDmPhong()
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.GetDmPhong();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        static public DataTable CreateNewPhong()
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.CreateNewPhong();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        static public string GetMaPhong(int tang, int vitri)
        {
            string maphong = string.Empty;
            try
            {
                maphong = Models.PhongModel.GetMaPhong(tang, vitri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return maphong;
        }

        //Lấy số lượng tầng
        static public int GetSoLuongTang()
        {
            int n = 0;
            try
            {
                n = Models.PhongModel.GetSoLuongTang();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return n;
        }

        //Lấy vị trí phòng MAX
        static public int GetViTriPhongMax()
        {
            int n = 0;
            try
            {
                n = Models.PhongModel.GetViTriPhongMax();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return n;
        }


        //Lấy vị trí phòng MAX
        static public int GetViTriPhongMax(int tang)
        {
            int n = 0;
            try
            {
                n = Models.PhongModel.GetViTriPhongMax(tang);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return n;
        }
        //Lấy tầng
        static public DataTable GetTang()
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.GetTang();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        //Lấy loại phòng
        static public DataTable GetLoaiPhong()
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.GeLoaiPhong();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        //Lấy tình trạng
        static public DataTable GetTinhTrangPhong()
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.getDmTinhTrang();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        //Thay đổi trạngth ái của phòng
        static public void ThayDoiTrangThaiPhong(string maphong, int tt)
        {
            Models.PhongModel.ThayDoiTrangThaiPhong(maphong, tt);
        }

        //Lấy vị trí
        static public DataTable GetViTriPhong(int tang)
        {
            DataTable tb = new DataTable();
            try
            {
                tb = Models.PhongModel.GetViTriPhong(tang);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return tb;
        }

        //Lấy giá tiền theo giờ
        static public double GetGiaTienTheoGioVND(string maLoaiPhong)
        {
            double result = 0;
            try
            {
                result = Models.PhongModel.GetGiaTienTheoGioVND(maLoaiPhong);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return result;
        }


        //Lấy giá tiền theo giờ
        static public double GetGiaTienTheoNgayVND(string maLoaiPhong)
        {
            
            double result = 0;
            try
            {
                result = Models.PhongModel.GetGiaTienTheoNgayVND(maLoaiPhong);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return result;
        }

        //Tính giá Ngoại tệ
        static public double TinhGiaNgoaiTe(double GiaVND,string ma_nt)
        {
            double result = 0;
            try
            {
                result = Models.PhongModel.TinhGiaNgoaiTe(GiaVND,ma_nt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return result;
        }

        //Kiểm tra phòng tồn tại
        static public bool KiemTraPhongTonTai(string ma_phong)
        {
            bool kt = false;
            try
            {
                kt = Models.PhongModel.KiemTraPhongTonTai(ma_phong);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return kt;
        }

        //Kiểm tra vị trí đã có phòng
        static public bool KiemTraViTriKhongHopLe(int tang, int vi_tri)
        {
            bool kt = false;
            try
            {
                kt = Models.PhongModel.KiemTraViTriKhongHopLe(tang, vi_tri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
            }
            return kt;
        }

        //thêm phong
        static public bool ThemPhong(DataTable tb)
        {
            try
            {
                Models.PhongModel.ThemPhong(tb);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "HotelManager", MessageBoxButton.OK, MessageBoxImage.Error);
                if (Models.DataProviderModel.IsOpen)
                    Models.DataProviderModel.Close();
                return false;
            }
            return true;
        }

        //Xóa loại phòng
        static public bool XoaPhong(string ma)
        {
            try
            {
                Models.PhongModel.XoaPhong(ma);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(SysLib.LanguageLib.GetLanguage().Equals("V")?"Phòng đang được sử dụng. không thể xóa!":
                        "Cannot delete. This room is being used", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }
    }
}
